﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace StockMentor
{
    public partial class frmDateFirma : Form
    {
        SqlConnection conn;
        DataSet dsFirma;
        CurrencyManager crManager;
        public frmDateFirma(SqlConnection conn)
        {
            InitializeComponent();
            this.conn = conn;
        }

        private void btnInchide_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnModifica_Click(object sender, EventArgs e)
        {
            //EnableDisableControls(true);

        }

        private void EnableDisableControls(bool bEnable)
        {
            txtAdresa.Enabled = bEnable;
            txtAdresaWeb.Enabled = bEnable;
            txtCapSocial.Enabled = bEnable;
            txtCif.Enabled = bEnable;
            txtDenFirma.Enabled = bEnable;
            txtEmail.Enabled = bEnable;
            txtFax.Enabled = bEnable;
            txtNrFact.Enabled = bEnable;
            txtRegCom.Enabled = bEnable;
            txtSerieFact.Enabled = bEnable;
            txtNumarNir.Enabled = bEnable;
            txtSerieNir.Enabled = bEnable;
            txtTelefon.Enabled = bEnable;
            rbDa.Enabled = bEnable;
            rbNu.Enabled = bEnable;
            
        }

        private void btnSalveaza_Click(object sender, EventArgs e)
        {
            if (txtDenFirma.Text.Length == 0)
            {
                MessageBox.Show("Completati denumirea firmei!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDenFirma.Focus();
                return;
            }

            if (txtCif.Text.Length == 0)
            {
                MessageBox.Show("Completati campul CIF!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCif.Focus();
                return;
            }
            if (txtRegCom.Text.Length == 0)
            {
                MessageBox.Show("Completati Registrul Comertului!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtRegCom.Focus();
                return;
            }
            if (txtAdresa.Text.Length == 0)
            {
                MessageBox.Show("Completati adresa!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAdresa.Focus();
                return;
            }
            if (txtCapSocial.Text.Length == 0)
            {
                MessageBox.Show("Completati capitalul social!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCapSocial.Focus();
                return;
            }


            if (txtNumarNir.Text.Length == 0)
            {
                MessageBox.Show("Completati numarul de la care pornesc Receptiile!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNumarNir.Focus();
                return;
            }
            if (txtSerieNir.Text.Length == 0)
            {
                MessageBox.Show("Completati seria Receptiilor!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSerieNir.Focus();
                return;
            }
            if (txtNrFact.Text.Length == 0)
            {
                MessageBox.Show("Completati numarul de la care pornesc Facturile!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNrFact.Focus();
                return;
            }
            if (txtSerieFact.Text.Length == 0)
            {
                MessageBox.Show("Completati seria facturilor!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSerieFact.Focus();
                return;
            }
            try
            {
                //EnableDisableControls(false);
                CustomProcedures.UPDATEFIRMA(System.DateTime.Now,
                                             this.txtDenFirma.Text,
                                             this.txtCif.Text,
                                             this.txtRegCom.Text,
                                             this.txtSerieNir.Text,
                                             Convert.ToInt32(this.txtNumarNir.Text),
                                             this.txtSerieFact.Text,
                                             Convert.ToInt32(this.txtNrFact.Text),
                                             this.txtTelefon.Text,
                                             this.txtFax.Text,
                                             this.txtEmail.Text,
                                             this.txtAdresaWeb.Text,
                                             this.txtAdresa.Text,
                                             Convert.ToDouble(this.txtCapSocial.Text),
                                             this.rbDa.Checked ? "1" : "0");
                MessageBox.Show("Salvarea s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("Modificarea nu s-a inregistrat.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void frmDateFirma_Load(object sender, EventArgs e)
        {
            //EnableDisableControls(false);
            CustomProcedures.conn = this.conn;

            dsFirma = new DataSet();
            SqlDataAdapter daFirma = new SqlDataAdapter("SELECT TOP 1 * FROM firma ORDER BY firma_data DESC", conn);
            daFirma.Fill(dsFirma, "firma");

            if (dsFirma.Tables[0].Rows.Count > 0)
            {
                txtNrFact.Enabled = false;
                txtNumarNir.Enabled = false;
                txtSerieFact.Enabled = false;
                txtSerieNir.Enabled = false;
            }

            SetControlDataBinding(dsFirma);

            crManager = (CurrencyManager)BindingContext[dsFirma.Tables["firma"]];
           
        }

        private void SetControlDataBinding(DataSet dsFirma)
        {
            txtDenFirma.DataBindings.Clear();
            txtCif.DataBindings.Clear();
            txtRegCom.DataBindings.Clear();
            txtSerieNir.DataBindings.Clear();
            txtNumarNir.DataBindings.Clear();
            txtSerieFact.DataBindings.Clear();
            txtNrFact.DataBindings.Clear();
            txtTelefon.DataBindings.Clear();
            txtFax.DataBindings.Clear();
            txtEmail.DataBindings.Clear();
            txtAdresa.DataBindings.Clear();
            txtAdresaWeb.DataBindings.Clear();
            txtCapSocial.DataBindings.Clear();

            txtDenFirma.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_denumire");
            txtCif.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_cif");
            txtRegCom.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_regcom");
            txtSerieNir.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_recep_serie");
            txtNumarNir.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_recep_nr");
            txtSerieFact.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fact_serie");
            txtNrFact.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fact_nr");
            txtTelefon.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_tel");
            txtFax.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_fax");
            txtEmail.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_email");
            txtAdresa.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_adresa");
            txtAdresaWeb.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_web");
            txtCapSocial.DataBindings.Add("Text", dsFirma.Tables["firma"], "firma_capsoc");

            if (dsFirma.Tables["firma"].Rows.Count > 0)
            rbDa.Checked = dsFirma.Tables["firma"].Rows[0]["firma_tva"].Equals("1");

            rbNu.Checked = (!rbDa.Checked);

       
        }

        private void btnAnuleaza_Click(object sender, EventArgs e)
        {
            //EnableDisableControls(false);

            dsFirma = new DataSet();
            SqlDataAdapter daFirma = new SqlDataAdapter("SELECT TOP 1 * FROM firma ORDER BY firma_data DESC", conn);
            daFirma.Fill(dsFirma, "firma");

            SetControlDataBinding(dsFirma);

            crManager = (CurrencyManager)BindingContext[dsFirma.Tables["firma"]];
           
        }

        private void btnSterge_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommand sqlComm = new SqlCommand("DELETE FROM firma WHERE firma_cif ='" + txtCif.Text + "'", conn);
                SqlDataReader reader = sqlComm.ExecuteReader();
                MessageBox.Show("Stergerea s-a efectuat cu succes!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reader.Close();
                reader.Dispose();
                reader = null;
                txtAdresa.Text = txtAdresaWeb.Text = txtCapSocial.Text = txtCif.Text = txtDenFirma.Text = txtEmail.Text = txtFax.Text = txtNrFact.Text = txtNumarNir.Text = txtRegCom.Text = txtSerieFact.Text = txtSerieNir.Text = txtTelefon.Text = string.Empty;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nu puteti sterge inregistrarea curenta. Datele sunt folosite.", "", MessageBoxButtons.OK , MessageBoxIcon.Error );
            }
                  
        }

        private void txtCapSocial_Leave(object sender, EventArgs e)
        {
            if (!IsPositiveNumber(((TextBox)sender).Text))
            {
                MessageBox.Show("Introduceti o valoare valida pentru capitalul social!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtCapSocial.Focus();
            }
        }
        public bool IsPositiveNumber(String strNumber)
        {
            Regex objPattern = new Regex("^([1-9][0-9]*(\\,{0,1})?|(0\\,))[0-9]*$");

            return objPattern.IsMatch(strNumber);
        }
    }
}
